package com.demo.ocr;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * TODO 使用图片生成 PDF 文件
 * @author 李德才
 * @version V2.0
 * @date 2021/7/15 14:23
 */

public class ImageToPdf {

  private static final String IMAGE = "C:\\Users\\86159\\Desktop\\resoult\\test.png";
  private static final String OUT_PDF = "C:\\Users\\86159\\Desktop\\resoult\\resoultpdf.pdf";

  public static void main(String[] args) {
    convert(IMAGE, OUT_PDF);
  }

  public static void convert(String source, String target) {
    Document document = new Document();
    // 设置文档页边距
    document.setMargins(0, 0, 0, 0);
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(target);
      PdfWriter.getInstance(document, fos);
      // 打开文档
      document.open();
      // 获取图片的宽高
      Image image = Image.getInstance(source);
      float imageHeight = image.getScaledHeight();
      float imageWidth = image.getScaledWidth();
      // 设置页面宽高与图片一致
      Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
      document.setPageSize(rectangle);
      // 图片居中
      image.setAlignment(Image.ALIGN_CENTER);
      // 新建一页添加图片
      document.newPage();
      document.add(image);
    } catch (Exception ioe) {
      System.out.println(ioe.getMessage());
    } finally {
      // 关闭文档
      document.close();
      try {
        assert fos != null;
        fos.flush();
        fos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
